package com.aaronprj.common.entities;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.aaronprj.common.entities.mbooks.BookItem;
import com.aaronprj.common.utils.CommonEntityManager;

public class SgmenuEntityManager extends CommonEntityManager{

	
	public static void saveItem(Object obj,boolean isNew){
		verifyEntityManager();
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		if(isNew){
			em.persist(obj);
		}else{
			em.merge(obj);
		}
		em.flush();
		
		em.getTransaction().commit();
		em.close();
	}
	
	public static <T> void deleteItem(Class<T> entityClass, Object primaryKey){
		verifyEntityManager();
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();

		T obj = em.find(entityClass, primaryKey);
		em.remove(obj);
		em.flush();
		
		em.getTransaction().commit();
		em.close();
	}

	public static void saveItems(Object[] objs){
		verifyEntityManager();
		EntityManager em = emf.createEntityManager();
		em.getTransaction().begin();
		for(Object obj:objs){
			em.persist(obj);
			em.flush();
		}
		
		em.getTransaction().commit();
		em.close();
	}
	
	public static <T> T findItem(Class<T> entityClass, Object primaryKey){

		verifyEntityManager();

		EntityManager em = emf.createEntityManager();
		T obj = em.find(entityClass, primaryKey);
		em.close();
		
		return obj;
	} 
	
	public static List<?> getAllItems(String entityName){

		verifyEntityManager();

		EntityManager em = emf.createEntityManager();
		List<?> objs = em.createNamedQuery(entityName+".findAll").getResultList();
		em.close();
		
		return objs;
	} 
	
	public static Object getItem(String namedQuery, String paramName,Object paramValue){

		List<?> objs = getEntities(namedQuery, paramName, paramValue);
		if(null == objs || objs.isEmpty()){
			return null;
		}
		return objs.get(0);
	} 

	
	public static List<?> getItems(String namedQuery, String paramName,Object paramValue){

		verifyEntityManager();

		EntityManager em = emf.createEntityManager();
		List<?> objs = em.createNamedQuery(namedQuery).setParameter(paramName, paramValue).getResultList();
		em.close();
		
		return objs;
	} 
	
	
	public static List<BookItem> getItemsFullText(String keywords){

		verifyEntityManager();

		EntityManager em = emf.createEntityManager();
		String[] keys;
		if(keywords.indexOf(" ") > -1){
			keys = keywords.split(" ");
		}else if(keywords.indexOf(",") > -1){
			keys = keywords.split(",");
		}else{
			keys = new String[]{keywords};
		}

		List<BookItem> objs = new ArrayList<BookItem>();
		
		for(int i = 0; i<keys.length; i++){
			String key = keys[i].trim();
			if(key.length() > 0){
				String queryString = "SELECT Distinct e FROM BookItem e WHERE " +
				"UPPER(e.name) LIKE :keyword or " +
				"UPPER(e.discription) LIKE :keyword or " +
				"UPPER(e.author) LIKE :keyword or " +
				"UPPER(e.isdn) LIKE :keyword " +
				"ORDER BY e.name";

				Query query = em.createQuery(queryString);
				query.setParameter("keyword", "%" + keys[i].toUpperCase() + "%");
				
				List<BookItem> tmpobjs = query.getResultList();
				for(BookItem o:tmpobjs){
					if(!objs.contains(o)) objs.add(o);
				}

			}
		}

		
		em.close();
		
		return objs;
	} 
	
	
	public static List<?> getItems(String queryString){

		verifyEntityManager();

		EntityManager em = emf.createEntityManager();
		List<?> objs = em.createQuery(queryString).getResultList();
		em.close();
		
		return objs;
	} 
	

	public static int batchUpdateItems(String updatequery){

		verifyEntityManager();

		EntityManager em = emf.createEntityManager();
		int ireturn = em.createQuery(updatequery).executeUpdate();
		em.close();
		return ireturn;
		
	} 
}
